const Sale = require('../models/sales.model');

//新
exports.create = function(req, res, next) {
    const sale = new Sale(req.body);
    sale.save().then(data => {
        res.json(data)
    })
}
//删
exports.remove = function(req, res, next) {
var id = req.params.id;
Sale.findByIdAndDelete(id, (err, data) => {
    res.json(data)
})
}
exports.removes = (req, res, next) => {
let ids = req.body["ids"];
Sale.remove({ _id: { $in: ids } }, (err, data) => {
  if (err) {
    console.log(err);
  } else {
    res.json(data);
  }
});
}

//改
exports.update = function(req, res, next) {
var id = req.params.id;
Sale.findByIdAndUpdate(id, { $set: req.body }, { new: false }).then(data => {
    res.json(data);
})
}
exports.getData = function(req, res, next) {
    var id = req.params.id;
    Sale.findById(id).then(data => {
        res.json(data);
    })
}
//查
exports.list = function(req, res, next) {
var page = req.body.page ? req.body.page : 1;
var rows = req.body.rows ? req.body.rows : 10;
var queryCondition = {};
if (req.body.name && req.body.name.trim().length > 0) {
    name = req.body.name;
    queryCondition = {
        name: new RegExp(name, 'i')
    };
}
Sale.paginate(queryCondition, { page: +page, limit: +rows }, function(err, result) {
    result.rows = result.docs;
    delete result.docs;
    res.json(result);
});
}
